﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TimeTracker;
using TimeTracker.Provider;
using Utility;
using Utility.Windows.Forms;
using TimeTracker.SkinProvider;

namespace TimeTracker.DefaultSkinProvider
{
    public partial class Provider : TimeTracker.SkinProvider.IProvider
    {
        void SkinProvider.IProvider.InitializeTreeView(System.Windows.Forms.TreeView treeView)
        {
            ImageList imageList = new ImageList();

            imageList.Images.Add("projects", Resource1.ProjectsIcon);
            imageList.Images.Add("project", Resource1.ProjectIcon);
            imageList.Images.Add("session", Resource1.SessionIcon);
            imageList.Images.Add("note", Resource1.NoteIcon);
            imageList.Images.Add("date", Resource1.DateIcon);
            imageList.Images.Add("dates", Resource1.ProjectsIcon);

            treeView.ImageList = imageList;
        }

        void SkinProvider.IProvider.SkinTreeNode(System.Windows.Forms.TreeNode treeNode, GroupByEnumeration groupBy)
        {
            treeNode.TreeView.SafeInvoke(delegate
            {
                treeNode.Text = this.GetNodeText(treeNode, groupBy);
                treeNode.ImageKey = treeNode.SelectedImageKey = this.GetNodeImageKey(treeNode);
            });
        }

        protected string GetNodeText(TreeNode node, GroupByEnumeration groupBy)
        {
            IFormatProvider timeSpanFormatProvider = new TimeSpanFormatProvider();

            if (node.Tag is IEnumerable<IProject>)
            {
                return node.SessionDuration().ToNiceString();
            }
            else if (node.Tag is IProject)
            {
                IProject project = (IProject)node.Tag;

                return String.Format("{0} ({1})", project.Name, node.SessionDuration().ToNiceString());
            }
            else if (node.Tag is ISession)
            {
                ISession session = (ISession)node.Tag;

                TreeNode parentDateNode = node.GetParentWhere(r => r.Tag is DateTime);

                string format = parentDateNode != null ? "{0:H:mm:ss} ({1})" : "{0} ({1})";

                TimeSpan duration = node.SessionDuration();

                return String.Format(format, session.StartTime, duration.ToNiceString());
            }
            else if (node.Tag is INote)
            {
                INote note = (INote)node.Tag;

                return String.Format("{0:H:mm:ss} {1}", note.Time, note.Text);
            }
            else if (node.Tag is DateTime)
            {
                DateTime day = (DateTime)node.Tag;

                return String.Format("{0:M/d/yyyy} ({1})", day, node.SessionDuration().ToNiceString());
            }
            else if (node.Tag is IEnumerable<DateTime>)
            {
                return String.Format("({0})", node.SessionDuration().ToNiceString());
            }

            return String.Empty;
        }

        protected string GetNodeImageKey(TreeNode node)
        {
            if (node.Tag is IEnumerable<IProject>)
            {
                return "projects";
            }
            else if (node.Tag is IProject)
            {
                return "project";
            }
            else if (node.Tag is ISession)
            {
                return "session";
            }
            else if (node.Tag is INote)
            {
                return "note";
            }
            else if (node.Tag is DateTime)
            {
                return "date";
            }
            else if (node.Tag is IEnumerable<DateTime>)
            {
                return "dates";
            }

            return String.Empty;
        }
    }
}
